﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BaseTool
{
    public class CommonHelper
    {
        /// <summary>
        /// 延时
        /// </summary>
        /// <param name="millisecond">毫秒</param>
        public static void Delay(int millisecond)
        {
            DateTime time = DateTime.Now.AddMilliseconds(millisecond);
            while (DateTime.Now < time)
            {
                if (millisecond < 1000)
                {
                    Thread.Sleep(10);
                }
                if (millisecond > 1000)
                {
                    Thread.Sleep(100);
                }
            }
        }
        /// <summary>  
        /// 获取时间戳  13位
        /// </summary>  
        /// <returns></returns>  
        public static long GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds * 1000);
        }
        /// <summary>
        /// 字符串转16进制数组
        /// </summary>
        /// <param name="strings"></param>
        /// <returns></returns>
        public static byte[] StringToBytes(string strings)
        {
            try
            {
                byte[] buffer = null;
                string transstring = strings.Replace(" ", "");
                if (string.Empty != transstring)
                {
                    uint numBytesToWrite = uint.Parse((transstring.Length / 2).ToString());
                    buffer = new byte[numBytesToWrite];


                    // Convert each hex byte string to a BYTE value
                    for (int i = 0; i < numBytesToWrite; i++)
                    {
                        var hexByte = Convert.ToInt32(transstring.Substring(i * 2, 2), 16);
                        buffer[i] = Convert.ToByte(hexByte);
                    }
                    //return buffer;
                }
                return buffer;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 16进制数组转字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToString(byte[] bytes)
        {
            try
            {
                string str = "";
                foreach (var item in bytes)
                {
                    str += item.ToString("X").PadLeft(2, '0') + " ";
                }
                return str.Trim();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// byte数组对比
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool EqualBytes(byte[] a, byte[] b)
        {
            if (a.Length != b.Length)
            {
                return false;
            }
            bool result = true;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i])
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
    }
}
